package de.carcophan.jimdb.jsf.security.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Base64;

public class PasswordEncrypter {

    public static String getHashFor(final String klartext) {

        try {
            final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            final byte[] passwordAsBytes = klartext.getBytes("ISO-8859-1");
            messageDigest.update(passwordAsBytes);
            final byte[] hash = messageDigest.digest();
            final byte[] base64Hash = Base64.encodeBase64(hash);
            final String result = new String(base64Hash, "ISO-8859-1");
            return result;
        } catch (NoSuchAlgorithmException e) {
            throw new CryptoException(e);
        } catch (UnsupportedEncodingException e) {
            throw new CryptoException(e);
        }
    }
}
